package com.baikeyang.service.WxUser.impl;

import cn.hutool.core.util.StrUtil;
import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.bean.activity.SimpleActivityUser;
import com.baikeyang.common.constant.CommonConstants;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.WxUser;
import com.baikeyang.common.util.shiro.ShiroUtil;
import com.baikeyang.mapper.WxUserMapper;
import com.baikeyang.service.WxUser.IWxUserService;
import com.baikeyang.service.base.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 微信用户 服务实现类
 * </p>
 *
 * @author BaiKeyang
 * @since 2020-05-22
 */
@Service
public class IWxUserServiceImpl extends SuperServiceImpl<WxUserMapper, WxUser> implements IWxUserService {

    @Autowired
    private WxUserMapper wxUserMapper;

    public WxUser getWxUser(String loginName) {
        QueryWrapper<WxUser> queryWrapper = Wrappers.query();
        queryWrapper.eq("login_name", loginName);
        return wxUserMapper.selectOne(queryWrapper);
    }

    @Override
    public Long saveWxUser(WxUser wxUser) {
        WxUser oUser = getWxUser(wxUser.getLoginName());
        if(null == oUser){
            if(StrUtil.isEmpty(wxUser.getName())){
                wxUser.setName("wx"+System.currentTimeMillis());
            }
            wxUser.setStatus(CommonConstants.DATA_STATUS_VALID);
            wxUser.setCreateTime(new Date());
            wxUser.setCreateUser(0L);
            wxUser.setUpdateTime(new Date());
            wxUser.setUpdateUser(0L);
            int flag = wxUserMapper.insert(wxUser);
            if(flag > 0){
                return wxUser.getId();
            }else {
                return 0L;
            }
        }
        return oUser.getId();
    }

    @Override
    public WxUser getWxUserByOpenId(String openId) {
        return getWxUser(openId);
    }

    @Override
    public IPage<SimpleActivityUser> selectActivityUserCares(Page<SimpleActivityUser> pageParam, Long userId) {
        IPage<SimpleActivityUser> activityUserIPage = wxUserMapper.selectCareSimpleActivityUser(pageParam, userId);
        return activityUserIPage;
    }

    @Override
    public ResponseResult updateWxUserById(WxUser wxUser) {
        if(null != wxUser.getUserId()){
            WxUser user = wxUserMapper.selectById(wxUser.getUserId());
            if(null != user){
                if(StrUtil.isNotEmpty(wxUser.getName())){
                    user.setName(wxUser.getName());
                }

                if(null != wxUser.getGender()){
                    user.setGender(wxUser.getGender());
                }

                if(StrUtil.isNotEmpty(wxUser.getAvatarUrl())){
                    user.setAvatarUrl(wxUser.getAvatarUrl());
                }
                int flag = wxUserMapper.updateById(user);
                if(flag > 0){
                    return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
                }
            }
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

}
